package me.zhengjie.modules.kaoqin.repository;

import me.zhengjie.modules.kaoqin.domain.Kaoqin;
import me.zhengjie.modules.kaoqin.domain.attention;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public interface AttentionRepository  extends JpaRepository<attention, Integer>, JpaSpecificationExecutor<attention> {
    public default List<Map<String, Object>> getAttendanceByStudentId(Integer studentid) {
        // 假设通过 studentid 查询数据库中的考勤记录
        attention record = findByStudentid(studentid);
        // attention attention = record.get();
        List<Map<String, Object>> rules = new ArrayList<>();
        // 修改课时数
        int count = 25; // 修改赋值动态调整课时数
        for (int i = 1; i <= record.getSum(); i++) {
            try {
                // 动态构造字段名
                String fieldName = "data" + i;
                // 获取字段
                Field field = attention.class.getDeclaredField(fieldName);
                // 设置字段为可访问
                field.setAccessible(true);
                // 获取字段值
                Integer value = (Integer) field.get(record);
                // 转换考勤状态
                String status = getAttendanceStatus(value);

                // Create a new map for each attendance entry
                Map<String, Object> attendanceData = new HashMap<>();
                attendanceData.put("id", i);
                attendanceData.put("value", status);

                // Add the map to the list
                rules.add(attendanceData);
                System.out.println(attendanceData);  // Print each entry for debugging
            } catch (NoSuchFieldException | IllegalAccessException e) {
                e.printStackTrace(); // 可以用日志记录替代
            }
        }
        return rules;
    }
    attention findByStudentid(Integer studentid);

    // 假设考勤状态描述为以下映射
    default String getAttendanceStatus(Integer value) {
        switch (value) {
            case 1:
                return "正常";
            case 2:
                return "迟到";
            case 3:
                return "缺席";
            default:
                return "病假";
        }
    }
}
